
import { ResultState, SDKLogicState, SDKState } from "../../SDKDefine";
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";

// import ToastController from "../../logic/toast/ToastController";
//https://open.oppomobile.com/wiki/doc#id=10537
//创建激励视频广告组件，全局单例，如果创建新的广告位 Ad 对象，会导致之前的 Ad 被销毁
@registerClass('OppoRewardAd')
export default class OppoRewardAd extends BaseAd {
    hide(): void {
    }
    load(): void {
        if (this.ad) {
            this.ad.load();
        }
    }

    onClose(res: any) {
        console.log('NativeTest onClose ', res)
        if (res && res.isEnded || res === undefined) {
            this.callResultCallback(ResultState.YES)
        } else {
            this.callResultCallback(ResultState.NO)
        }
    }
    create(): void {
        if (!this.ad) {
            this.ad = this.sdk.createRewardedVideoAd({ adUnitId: this.adUnitID })
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onClose(this.getFunc(this.onClose))
            console.log(' 创建成功')
        } else {
            this.ad.load();
        }
    }

    init() {
        // this.preload(SDKState.hide)
        this.preload();
    }

    // preload(s: SDKState) {
    //     this.setLogicState(s)
    //     this.setState(SDKState.loading)
    //     this.create();
    // }

    show() {
        this.ad.show().then(() => {
            console.log('激励视频广告展示完成');
        }).catch((err: any) => {
            console.log('激励视频广告展示失败', JSON.stringify(err));
        })
    }

    destroy() {

    }
}